<?php

namespace App\Http\Service;

use App\Util\RandomHelper;
use App\Util\Result;
use App\Model\AuthNode;
use App\Model\Shelf;
use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

trait ServiceShelf{

    /**
     * czs
     * @return mixed
     * 柜子列表
     */
    public static function getList($keyWord,$limit){
        if (empty($keyWord)) {
            $list = DB::table('shelves as a')
                ->leftjoin('regions as b','a.cid','=','b.id')
                ->leftjoin('regions as c','b.pid','=','c.id')
                ->leftjoin('regions as d','c.pid','=','d.id')
                ->select('a.name','b.name as bname','c.name as cname','d.name as dname','a.address','a.longi','a.lati','a.number','a.id','a.type')
                ->paginate($limit);
        } else {
            $list = Shelf::where("name","like", '%'.$keyWord.'%')->orderBy('id','desc')->paginate($limit);
        }
        return Result::success()->setData($list);
    }

    /**
     * @return mixed
     * 父节点列表
     */
    public static function getLists(){
        $res = AuthNode::where('nid',0)->get()->toarray();
        return Result::success()->setData($res);

    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = FunctionRule::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新记录
     * czs
     */
    public static function updateData($input, $id) {
        $input['start_time'] = strtotime($input['start_time']);
        $input['end_time'] = strtotime($input['end_time'])+24*3600-1;
        $res = FunctionRule::where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    public static function getDoorList($id,$limit){
        $model = DB::table('doors as a');
        $list = $model
            ->leftjoin('shelves as b','b.id','=','a.sid')
            ->where('a.sid',$id)
            ->select(DB::raw('a.status,a.created_at,a.code,a.id,a.column,b.number,b.type'))
            ->paginate($limit);
        return Result::success()->setData($list);
    }

    public static function getDoorBooks($keyWord,$id,$limit){
        $model = DB::table('booksindoor as a');
        if (!empty($keyWord)) {
            $model->where(function($query) use($keyWord) {
                $query->where("b.Title","like", '%'.$keyWord.'%')
                    ->orwhere('e.Content',"like", '%'.$keyWord.'%');
            });
        }
        $list = $model
            ->leftjoin('doors as h','h.code','=','a.DoorID')
            ->where('h.id',$id)
            ->leftjoin('TagInfo as b','a.BookEpc','=','b.rUID')
            ->leftjoin('Items as c','c.BarCode','=','b.Barcode')
            ->leftjoin('circ_marc as d','d.MarcID','=','c.MarcID')
            ->leftjoin('ttindex7 as e','d.IID','=','e.ID')
            ->select('a.BookEpc','a.InTime','d.Title','e.Content')
            ->paginate($limit);
        return Result::success()->setData($list);
    }


    public static function yieldDoors($num,$id,$number)
    {
        $data['status'] = 1;
        $data['book_status'] = 2;
        $data['sid'] = $id;
        $data['created_at'] = time();
        for ($i=1;$i<$num+1;$i++) {
            $data['code'] =  RandomHelper::randomString(6);
            $data['column'] = $i+1;
            //生成二维码
            QrCode::format('png')->size(8)->generate($number.$data['code'].$i,public_path("uploads/qrcode{$data['code']}_8.png"));
            QrCode::format('png')->size(12)->generate($number.$data['code'].$i,public_path("uploads/qrcode{$data['code']}_12.png"));
            QrCode::format('png')->size(15)->generate($number.$data['code'].$i,public_path("uploads/qrcode{$data['code']}_15.png"));
            QrCode::format('png')->size(30)->generate($number.$data['code'].$i,public_path("uploads/qrcode{$data['code']}_30.png"));
            QrCode::format('png')->size(50)->generate($number.$data['code'].$i,public_path("uploads/qrcode{$data['code']}_50.png"));


            DB::table('doors')->insert($data);
        }
        return Result::success();
    }
    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        $res = DB::table('shelves')->where("id", $id)->delete();
        return Result::success()->setData($res);
    }

    /**
     * 禁用、启用
     * czs
     */
    public static function able($id,$able) {
        FunctionRule::where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("启用")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * 规则下拉
     * czs
     */
    public static function functionPull() {
        $res = FunctionRule::where('status',1)->select('id', 'name')->get();
        return Result::success()->setData($res);
    }
    
    public static function getByName($name) {
    	$res = FunctionRule::where('name',$name)->select('id')->get();
    	return Result::success()->setData($res);
    }
    
}


